import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;

public class ScopeCoding {

    FileSystem fs = null;
    @Before
    public void init() throws Exception {
        Configuration conf = new Configuration();
//        conf.set("fs.defaultFS", "hdfs://10.211.55.8:9000");
//        conf.set("dfs.replication", "3");
        fs = FileSystem.get(new URI("hdfs://10.211.55.8:9000"), conf, "root");
    }
    @Test
    public void testCat() throws IOException {
        FSDataInputStream in = fs.open(new Path("/input/test.txt"));
        FileStatus[] listStatus = fs.listStatus(new Path("/input/test.txt"));
        BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(listStatus[0], 0L, listStatus[0].getLen());
        long length = fileBlockLocations[0].getLength();
        long offset = fileBlockLocations[0].getOffset();
        System.out.println(length);
        System.out.println(offset);
        byte[] b = new byte[4096];
        FileOutputStream os = new FileOutputStream(new File("src/output/scopecode.txt"));
        while (in.read(offset, b, 0, 4096) == -1) {
            os.write(b);
            offset += 4096;
            if (offset >= length) {
                return;
            }
        }
        os.flush();
        os.close();
        in.close();
    }
}
